$(function () {
    var mcc = document.querySelector(".message-center-container");
    var messageList = mcc.querySelector(".message-list");
    var mt = mcc.querySelector(".message-tip");
    var ma = mcc.querySelector(".message-area");
    var mda = ma.querySelector(".message-down-arrow");
    var showTimeout;
    function showMessageArea() {
        clearTimeout(showTimeout);
        ma.style.display = "block";
        var mt_width = mt.offsetWidth;
        var ma_width = ma.offsetWidth;
        var mt_rect = mt.getBoundingClientRect();
        var ma_rect = ma.getBoundingClientRect();
        var win_width = 0;
        if (window.innerWidth) {
            win_width = window.innerWidth;
        }
        else if (document.documentElement.clientWidth) {
            win_width = document.documentElement.offsetWidth;
        }
        else if (document.body.clientWidth) {
            win_width = document.body.clientWidth;
        }
        if (mt_rect.left <= ma_width / 2) {
            ma.style.left = -mt_rect.left + 20 + "px";
            mda.style.left = mt_rect.left - 20 + "px";
        }
        else {
            var mt_rect_right = win_width - mt_rect.left - mt_width;
            if (mt_rect_right <= ma_width / 2) {
                ma.style.left = -(ma_width - mt_rect_right - mt_width) - 20 + "px";
                mda.style.left = (ma_width - mt_rect_right - mt_width) + 20 + "px";
            }
            else {
                ma.style.left = -(ma_width - mt_width) / 2 + "px";
                mda.style.left = (ma_width - mt_width) / 2 + "px";
            }
        }
    }
    function hideMessageArea() {
        clearTimeout(showTimeout);
        showTimeout = setTimeout(function () {
            ma.style.display = "none";
        }, 100);
    }
    function closeMessageMask(obj) {
        $("body").removeClass("show-message-mask");
        $.ajax({
            url: "/Default/Services/HandleCenterMessage.ashx",
            method: "POST",
            data: { type: 'update', list: $(obj).data("messageid") },
            success: function (data) {
                if (data.succeed) {
                    //更新成功，后续操作
                }
            }
        });
    }
    mcc.onmouseenter = function () {
        showMessageArea();
    }
    mcc.onmouseleave = function () {
        hideMessageArea();
    }
    ma.onmouseenter = function () {
        clearTimeout(showTimeout);
    }
    ma.onmouseleave = function () {
        hideMessageArea();
    }
    $.ajax({
        url: "./data.js",
        method: "POST",
        data: {type:0},
        success: function (data) {
            if (data.succeed) {
                var tips_count = mcc.querySelectorAll(".tip-count");
                if (data.unreadTotalCount > 0) {
                    var tips_count = mcc.querySelectorAll(".tip-count");
                    var lis = "";
                    for (var i = 0; i < data.items.length; i++) {
                        var template = "<li><a href='http://www.ningmengyun.com/CenterMessage.aspx' class='greenHover'><div><span class='message-item-date'>{{0}}</span>&nbsp;&nbsp;<span class='message-item-text'>{{1}}</span></div></a></li>";
                        lis += template.replace("{{0}}", data.items[i].CREATED_DATE).replace("{{1}}", (data.items[i].TITLE != null && data.items[i].TITLE) ? (data.items[i].TITLE.length > 18 ? data.items[i].TITLE.substring(0, 18) + "..." : data.items[i].TITLE) : "");
                    }
                    messageList.innerHTML = lis;
                    for (var i = 0; i < tips_count.length; i++) {
                        tips_count[i].style.display = "inline-block";
                        tips_count[i].innerText = data.unreadTotalCount;
                    }
                    $("body").addClass("show-message-mask");
                    if (data.popup.HasPICTURE) {
                        var consoleUrl = "http://console.ningmengyun.com:5000";//测试：http://101.200.201.49:10102; 生产：http://console.ningmengyun.com
                        $(".content-column").css({ "display": "none" });
                        $(".message-mask .message-primary-img").attr({ "src": consoleUrl + data.popup.PICTURE });
                        if(data.popup.LINK && data.popup.LINK != "null"){
                            $(".message-mask .message-link").attr("href", decodeURIComponent(data.popup.LINK));
                        }      
                        $(".mask-close").data("messageid", data.popup.ANCM_SN);
                    }
                    else{
                        $(".message-mask .message-primary-img").css({"display":"none"});
                        $(".content-column").css({"display":"block"});
                        $(".content-column .column-title").text(data.items[0].TITLE);
                        $(".content-column .column-content").html(data.items[0].BODY);
                        $(".mask-close").data("messageid", data.items[0].ANCM_SN);
                    }
                    $(".mask-close").bind("click", function () {
                        closeMessageMask(this);
                    });
                }
                else {
                    mcc.querySelector(".no-message-text").style.display = "block";
                    for (var i = 0; i < tips_count.length; i++) {
                        tips_count[i].innerText = "0";
                    }
                }
            }
        }
    });
    var ajaxing = false;
    $(document).on("mousedown", "#divMainMenuContainer", function () {
        if (ajaxing) return;
        ajaxing = true;
        $.ajax({
            url: "./data2.js",
            method: "GET",
            success: function (data) {
                ajaxing = false;
                //console.log(data);
                if (appOnlineMessage && data.succeed && data.result && data.result.ANCM_SN != "-1") {
                    appOnlineMessage(data.result.TITLE, data.result.BODY);
                }
                else if (!data.succeed) {
                    //console.error(data.msg);
                }
            },
            erro: function () {
                ajaxing = false;
            }
        });
    });
});